package com.lsz.crm.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lsz.crm.base.BaseService;
import com.lsz.crm.mapper.CusDevPlanMapper;
import com.lsz.crm.mapper.SaleChanceMapper;
import com.lsz.crm.pojo.CusDevPlan;
import com.lsz.crm.pojo.SaleChance;
import com.lsz.crm.query.CusDevPlanQuery;
import com.lsz.crm.query.SaleChanceQuery;
import com.lsz.crm.utils.AssertUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author liushizhong
 * @Date 2021/10/12 20:30
 * @Version 1.0
 */

@Service
public class CusDevPlanService extends BaseService<CusDevPlan,Integer> {

    @Autowired
    private CusDevPlanMapper cusDevPlanMapper;

    @Autowired
    private SaleChanceMapper saleChanceMapper;

    /**
     * 根据多条件分页查询客户开发计划
     * @param cusDevPlanQuery
     * @return
     */
    public Map<String,Object> queryCusDevPlanByParams(CusDevPlanQuery cusDevPlanQuery){

        Map<String,Object> map = new HashMap<>();

        // 开启分页,参数为当前页码，与每页显示的条数
        PageHelper.startPage(cusDevPlanQuery.getPage(),cusDevPlanQuery.getLimit());
        // 得到分页对象
        PageInfo<CusDevPlan> pageInfo = new PageInfo<>(cusDevPlanMapper.selectByParams(cusDevPlanQuery));

        map.put("code",0);
        map.put("msg","success");
        // 总页数
        map.put("count",pageInfo.getTotal());
        // 设置分页好的列表
        map.put("data",pageInfo.getList());
        return map;
    }

    /**
     * 添加客户开发计划数据
     * @param cusDevPlan
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void addCusDevPlan(CusDevPlan cusDevPlan){
        /* 1.参数校验 */
        checkCusDevPlanParams(cusDevPlan);
        /* 2.设置默认参数 */
        cusDevPlan.setCreateDate(new Date());
        cusDevPlan.setUpdateDate(new Date());
        // 执行添加操作
        AssertUtil.isTrue(cusDevPlanMapper.insertSelective(cusDevPlan) != 1,"计划项数据添加失败！");
    }

    /**
     * 更新客户开发计划项
     * @param cusDevPlan
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void updateCusDevPlan(CusDevPlan cusDevPlan){
        // 1.执行参数检验
        // 计划项 ID 非空
        AssertUtil.isTrue(cusDevPlan.getId() == null ||
                cusDevPlanMapper.selectByPrimaryKey(cusDevPlan.getId()) == null,"数据异常，请重试！");
        checkCusDevPlanParams(cusDevPlan);
        // 2. 设置默认参数
        cusDevPlan.setUpdateDate(new Date());
        // 3. 执行更新操作
        AssertUtil.isTrue(cusDevPlanMapper.updateByPrimaryKeySelective(cusDevPlan) != 1,"开发计划项更新失败！");


    }

    /**
     * 参数校验
     * @param cusDevPlan
     */
    private void checkCusDevPlanParams(CusDevPlan cusDevPlan) {
        // 获取营销机会 id 非空判断
        Integer sId = cusDevPlan.getSaleChanceId();
        AssertUtil.isTrue(sId == null || saleChanceMapper.selectByPrimaryKey(sId) == null,"数据异常，请重试！");
        // 计划项，非空
        AssertUtil.isTrue(StringUtils.isBlank(cusDevPlan.getPlanItem()),"计划项内容不能为空！");
        // 计划时间，非空
        AssertUtil.isTrue(null == cusDevPlan.getPlanDate(),"计划时间不能为空！");
    }

    /**
     * 删除计划项
     * @param id
     */
    public void delCusDevPlan(Integer id) {
        // id 以及 记录非空验证
        AssertUtil.isTrue(null == id,"待删除记录不存在！");
        CusDevPlan cusDevPlan = cusDevPlanMapper.selectByPrimaryKey(id);
        AssertUtil.isTrue(cusDevPlan == null,"待删除记录不存在!");
        assert cusDevPlan != null;
        cusDevPlan.setIsValid(0);
        cusDevPlan.setUpdateDate(new Date());
        // 删除操作（更新状态）
        AssertUtil.isTrue(cusDevPlanMapper.updateByPrimaryKeySelective(cusDevPlan) != 1,"删除计划项失败！");
    }
}
